﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ColorPane.Control
{
    /// <summary>
    /// HSLColorPane.xaml 的交互逻辑
    /// </summary>
    public partial class HSLColorPane : UserControl
    {
        PixelHelper SLHelper;
        PixelHelper HueHelper;

        #region dependencyProperty
        bool Lock;
        public readonly static DependencyProperty HueProperty=
            DependencyProperty.Register("Hue",typeof(double),typeof(HSLColorPane),new PropertyMetadata(0d));
        public double Hue {
            get => (double) this.GetValue( HueProperty );
            set {
                this.SetValue( HueProperty, value );
                UpdateRGB( PixelHelper.HSV2RGB( Hue, Saturation, Light ) );
            }
        }

        public readonly static DependencyProperty SaturationProperty=
            DependencyProperty.Register("Saturation",typeof(double),typeof(HSLColorPane),new PropertyMetadata(0d));
        public double Saturation {
            get => (double) this.GetValue( SaturationProperty );
            set {
                this.SetValue( SaturationProperty, value );
                UpdateRGB( PixelHelper.HSV2RGB( Hue, Saturation, Light ) );
            }
        }

        public readonly static DependencyProperty LightProperty=
            DependencyProperty.Register("Light",typeof(double),typeof(HSLColorPane),new PropertyMetadata(0d));
        public double Light {
            get => (double) this.GetValue( LightProperty );
            set {
                this.SetValue( LightProperty, value );
                UpdateRGB( PixelHelper.HSV2RGB( Hue, Saturation, Light ) );
            }
        }


        public readonly static DependencyProperty RedProperty=
            DependencyProperty.Register("Red",typeof(double),typeof(HSLColorPane),new PropertyMetadata(0d));
        public double Red {
            get => (double) this.GetValue( RedProperty );
            set {
                this.SetValue( RedProperty, value );
                UpdateHSL( PixelHelper.RGB2HSV( Red, Green, Blue ) );
            }
        }

        public readonly static DependencyProperty AlphaProperty=
            DependencyProperty.Register("Alpha",typeof(double),typeof(HSLColorPane),new PropertyMetadata(0d));
        public double Alpha {
            get => (double) this.GetValue( AlphaProperty );
            set {
                this.SetValue( AlphaProperty, value );
            }
        }

        public readonly static DependencyProperty BlueProperty=
            DependencyProperty.Register("Blue",typeof(double),typeof(HSLColorPane),new PropertyMetadata(0d));
        public double Green {
            get => (double) this.GetValue( GreenProperty );
            set {
                this.SetValue( GreenProperty, value );
                UpdateHSL( PixelHelper.RGB2HSV( Red, Green, Blue ) );
            }
        }

        public readonly static DependencyProperty GreenProperty=
            DependencyProperty.Register("Green",typeof(double),typeof(HSLColorPane),new PropertyMetadata(0d));
        public double Blue {
            get => (double) this.GetValue( BlueProperty );
            set {
                this.SetValue( BlueProperty, value );
                UpdateHSL( PixelHelper.RGB2HSV( Red, Green, Blue ) );
            }
        }

        public readonly static DependencyProperty SoildBrushProperty=
            DependencyProperty.Register("SoildBrush",typeof(SolidColorBrush),typeof(HSLColorPane),new PropertyMetadata(new  SolidColorBrush(Colors.Red)) );
        public SolidColorBrush SoildBrush {
            get => ( new SolidColorBrush( (Color) this.GetValue( SoildBrushProperty ) ) );
            set {
                this.SetValue( SoildBrushProperty, value );
            }
        }

        #endregion

        public HSLColorPane()
        {
            InitializeComponent();
            SLHelper = new PixelHelper( SLPane, SLPaneCursor, true );
            SLHelper.OnCanDragMove += (x, y) =>
            {
                Light = 1-y;
                Saturation = x;
            };


            HueHelper = new PixelHelper( HuePane, HuePaneCursor, false );
            var Shader = SLPane.Effect as Effects.ColorShaderEffect;
            HueHelper.OnCanDragMove += (x1, y1) =>
            {                
                Hue = 360 * y1;
                Shader.PickColor = PixelHelper.HSL2RGB(Hue,1d,.5d).Color;
            };
        }

        static HSLColorPane()
        {

        }

        void UpdateRGB(SolidColorBrush brush)
        {
            if ( !Lock )
            {
                Lock = true;
                Red = brush.Color.R;
                Blue = brush.Color.B;
                Green = brush.Color.G;
                Alpha = 255;
                SoildBrush = brush;
                var color = brush.Color;
                color.A = (byte) Alpha;
                SoildBrush = brush;
                Lock = false;
            }
        }

        void UpdateHSL(Tuple<double, double, double> hsl)
        {
            if(!Lock)
            {
                Lock = true;
                Hue = hsl.Item1;
                Saturation = hsl.Item2;
                Light = hsl.Item3;
                Lock = false;
            }

        }
    }
}
